import logging

from datastore.resource import *
from datastore.uri import *
from runtime.service import *

class HelloWorld(Service):
    """
    A Simple service to log a message and generate a HTML page.
    """

    def cb_start(self, uri, action, res):
        """
        cb_start is where you setup process and watch callbacks that define 
        the API of the service.
        """
        self.logger.log(logging.INFO, "Hello World!")
        
        # Create our data resource
        page_res = Resource(type='term:base/examples/hello_world/data')
        page_res.dc_title("Hello World!")
        page_res.dc_description("This is a Hello World page.")
        page_res.data = "Yes I can."
        
        # Save our data Resource. When this is fetched, it will apply
        # the template defined in the definition resource
        # 'term:base/examples/hello_world/data'
        self.spaces.put(res.options.url, page_res)
        
        # Add this UI to the examples index page
        index_res = Resource(type='term:base/services/index/entry')
        index_res.dc_title("Hello World!")
        index_res.index = Uri('external:examples')
        index_res.uri = res.options.url
        self.spaces.put(Uri('internal:base/index/entries/'), index_res)
        

